package xc.doughnut.util;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.po.TableFill;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import xc.doughnut.service.IOracleService;

import java.util.Arrays;

/**
 * @descriptoon  MyBatis-Plus Oracle代码生成类
 * @author  XuChao
 */
public class OracleGenerator {

	// 作者
	private static final String AUTHOR = "XuChao";
	// 数据库账号
	public static final String DATA_SOURCE_USER_NAME = "SCOTT";
	// 数据库密码
	public static final String DATA_SOURCE_PASSWORD = "123456";
	// 数据库url
	public static final String DATA_SOURCE_URL = "jdbc:oracle:thin:@localhost:1521:orcl";
	// 生成的代码相对输出路径
	private static final String OUTPUT_DIR = "/src/main/java";
	// 设置目录结构,将生成的代码放在该目录下
	private static final String GENERATOR_PARENT_DIR = "com.egintra";
	// 生成的表,用`,`分割
	public static String[] tableNames = new String[] {
			"TABLE2"
	};
	// 是否将该库表全部生成
	private static final boolean ALL_TABLES_GENERATOR = false;
	// 默任包结构
	private static final String CONTROLLER_DEF = "controller";
	private static final String ENTITY_DEF = "entity";
	private static final String MAPPER_DEF = "mapper";
	private static final String SERVICE_IMPL_DEF = "service.impl";
	private static final String SERVICE_DEF = "service";

	// TODO 默认生成entity，需要生成DTO修改此变量
	// 一般情况下要先生成 DTO类 然后修改此参数再生成 PO 类。
	private static final Boolean IS_DTO = false;

	public static void main(String[] args) {
		// 代码生成器对象
		AutoGenerator mpg = new AutoGenerator();
		// 选择 freemarker 引擎，默认 Velocity
		mpg.setTemplateEngine(new FreemarkerTemplateEngine());
		// 全局配置
		GlobalConfig gc = new GlobalConfig();
		gc.setFileOverride(true);
		//生成路径
		gc.setOutputDir(System.getProperty("user.dir") + OUTPUT_DIR);
		gc.setAuthor(AUTHOR);
		gc.setOpen(false);
		gc.setSwagger2(false);
		gc.setServiceName("%sService");
        gc.setBaseResultMap(true);
        gc.setBaseColumnList(true);

		if (IS_DTO) {
			gc.setSwagger2(true);
			gc.setEntityName("%sDTO");
		}
		mpg.setGlobalConfig(gc);

		// 数据库配置
		DataSourceConfig dsc = new DataSourceConfig();
		dsc.setDbType(DbType.ORACLE);
		dsc.setUrl(DATA_SOURCE_URL);
		dsc.setDriverName("oracle.jdbc.driver.OracleDriver");
		dsc.setUsername(DATA_SOURCE_USER_NAME);
		dsc.setPassword(DATA_SOURCE_PASSWORD);
		mpg.setDataSource(dsc);

		// 包配置
		PackageConfig pc = new PackageConfig();
		pc.setParent(GENERATOR_PARENT_DIR);
		pc.setController(CONTROLLER_DEF);
		pc.setService(SERVICE_DEF);
		pc.setServiceImpl(SERVICE_IMPL_DEF);
		pc.setXml(MAPPER_DEF);
		pc.setEntity(ENTITY_DEF);
		mpg.setPackageInfo(pc);

		// 设置模板
		TemplateConfig tc = new TemplateConfig();
		mpg.setTemplate(tc);

		// 策略配置
		StrategyConfig strategy = new StrategyConfig();
		strategy.setNaming(NamingStrategy.underline_to_camel);
		strategy.setColumnNaming(NamingStrategy.underline_to_camel);
		// strategy.setSuperEntityClass("com.baomidou.mybatisplus.extension.activerecord.Model");
		strategy.setEntityLombokModel(true);
		strategy.setRestControllerStyle(true);
		strategy.setEntityLombokModel(true);
		if (ALL_TABLES_GENERATOR) {
			tableNames = new IOracleService().findAllTablesArr();
		}
		strategy.setInclude(tableNames);
		System.out.println("需要生成的表名: " + Arrays.toString(tableNames));
		strategy.setControllerMappingHyphenStyle(true);
		strategy.setTablePrefix(pc.getModuleName() + "_");
		mpg.setStrategy(strategy);
		mpg.setTemplateEngine(new FreemarkerTemplateEngine());
		mpg.execute();

		// 自动填充字段配置
		strategy.setTableFillList(Arrays.asList(
				new TableFill("create_date", FieldFill.INSERT),
				new TableFill("change_date", FieldFill.INSERT_UPDATE),
				new TableFill("modify_date", FieldFill.UPDATE)
		));
		mpg.setStrategy(strategy);
		mpg.execute();
	}
}


